/*
 * Copyright (c) dabing.io
 * All rights reserved.
 * ScheduleSettleTask.java
 */

package com.ueboot.weishop.admin.batch.schedule;

import com.ueboot.core.utils.DateUtils;
import com.ueboot.weishop.admin.batch.JobRunUtil;
import com.ueboot.weishop.admin.batch.WsJobParameters;
import com.ueboot.weishop.entity.finance.SettlementBatch;
import com.ueboot.weishop.service.finance.SettlementBatchService;
import com.ueboot.weishop.service.finance.SettlementDetailService;
import com.ueboot.weishop.service.finance.SettlementImportService;
import com.ueboot.weishop.service.weixin.WeiXinService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 描述: 定时任务
 *
 * @author Yvan
 * @since 1.0
 */
@Component
@Configurable
@EnableScheduling
public class ScheduleSettleTask {
	private static final Logger logger = LoggerFactory.getLogger(ScheduleSettleTask.class);

	/*private final SettlementBatchService settlementBatchService;

	private final SettlementImportService settlementImportService;

	private final SettlementDetailService settlementDetailService;

	private final WeiXinService weiXinService;

	private final JobLauncher jobLauncher;

	private final JobRepository jobRepository;

	private final Job jobSettle;
*/
	/**
	 * 对账文件下载存放路径
	 */
	@Value(value = "${settle.filePath}")
	private String settleFilePath;
/*

	@Autowired
	public ScheduleSettleTask(SettlementBatchService settlementBatchService, SettlementImportService settlementImportService, SettlementDetailService settlementDetailService, WeiXinService weiXinService, JobLauncher jobLauncher, JobRepository jobRepository, Job jobSettle) {
		this.settlementBatchService = settlementBatchService;
		this.settlementImportService = settlementImportService;
		this.settlementDetailService = settlementDetailService;
		this.weiXinService = weiXinService;
		this.jobLauncher = jobLauncher;
		this.jobRepository = jobRepository;
		this.jobSettle = jobSettle;
	}
*/


	//生成每日结算批次数据 每天执行一次
	@Scheduled(cron = "0/30 * * * * ?")
	public void executeCreateSettleBatch() {
		/*logger.info("创建每天需要执行结算任务的数据  开始......");
		this.createSettleBatch();
		logger.info("创建每天需要执行结算任务的数据  结束......");*/
	}


	//生成每日结算批次数据 每天执行一次
	@Scheduled(cron = "0/30 * * * * ?")
	public void executeSaveChatSessionBoardChartData() {
		/*logger.info("开始执行结算批处理任务");
		List<SettlementBatch> retval = settlementBatchService.findByStatusOrderByFirstInsertDesc(SettlementBatch.STATUS_INI);
		logger.info("当前共{}条记录需要处理", retval.size());
		for (SettlementBatch batch : retval) {
			JobParameters jobParameters = new JobParametersBuilder().addString(WsJobParameters.BATCH_NO, batch.getBatchNo()).toJobParameters();
			JobRunUtil.runJob(jobSettle, jobParameters, jobRepository, jobLauncher);
		}*/
	}

	/**
	 * 创建每天需要执行结算任务的数据
	 */
	/*private void createSettleBatch() {
		//当前时间减一天
		Date now = DateUtils.getNextDay(Calendar.getInstance().getTime(), -1);

		//查找最后的日期
		SettlementBatch batch = settlementBatchService.findTopByOrderBySettleDateDesc();
		if (batch != null) {
			//比较日期
			Long days = DateUtils.getBetweenDay(batch.getSettleDate(), now);
			//生成从上次到今天的所有日期
			for (Integer i = 0; i < days; i++) {
				saveMMBatch(DateUtils.getNextDay(batch.getSettleDate(), i + 1));
			}
		} else {
			saveMMBatch(now);
		}
	}

	private void saveMMBatch(Date date) {
		String fmtDate = DateUtils.format(date, DateUtils.FORMAT_DATE_DEFAULT);
		//创建新的任务
		SettlementBatch settlementBatch = new SettlementBatch();
		//去掉时分秒
		settlementBatch.setSettleDate(DateUtils.parseDate(fmtDate, DateUtils.FORMAT_DATE_DEFAULT));
		settlementBatch.setBatchNo(DateUtils.format(date, DateUtils.FORMAT_DATE_TIME_YYYYMMdd));
		settlementBatch.setFileName(fmtDate + ".txt");
		settlementBatch.setStatus(SettlementBatch.STATUS_INI);
		settlementBatchService.save(settlementBatch);
	}*/
}